#!/bin/bash -e

# project_root
NODE_ROOT=/usr/local/node
PROJECT_NAME=smfe
PROJECT_ROOT=$NODE_ROOT/$PROJECT_NAME

# tomcat_root
TOMCAT_ROOT=/usr/local/tomcat

# healthCheck
HEALTH_CHECK_ROOT=/usr/local/tomcat/webapps/ROOT
HEALTH_CHECK_PAGE=healthCheck.html

tomcat=$(ps -ef |grep tomcat |grep -w $TOMCAT_ROOT|grep -v 'grep'|awk '{print $2}')
echo "tomcat 进程ID:$tomcat"

if test -z $tomcat
then
  echo "tomcat 未开启，请先启动"
else
  echo "tomcat 运行中"
#  kill -9 $tomcat
fi

#删除tomcat下的源文件
rm -rf $TOMCAT_ROOT/webapps/$PROJECT_NAME

#复制项目静态文件到tomcat下
cp -r $PROJECT_ROOT $TOMCAT_ROOT/webapps

#如果webapps下无ROOT目录，新建
if ! test -d $HEALTH_CHECK_ROOT
then
  mkdir $HEALTH_CHECK_ROOT
fi

#如果心跳文件不存在，则复制原项目下的healthCheck文件
if ! test -f $HEALTH_CHECK_ROOT/$HEALTH_CHECK_PAGE
then
  cp $PROJECT_ROOT/$HEALTH_CHECK_PAGE $HEALTH_CHECK_ROOT
fi

#echo "tomcat 启动中..."
#$TOMCAT_ROOT/bin/startup.sh
